package com.cup.bddm.impala.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.dao.BranchDao;
import com.cup.bddm.impala.dao.NationDao;
import com.cup.bddm.impala.po.BranchDailyTrans;
import com.cup.bddm.impala.po.BranchGeneDailyTrans;
import com.cup.bddm.impala.service.NationService;
import com.cup.bddm.impala.vo.NationBranchTransVo;
import com.cup.bddm.impala.vo.NationGeneDailyTransVo;
import com.cup.bddm.impala.vo.TransDist;

@Service
public class NationService {
	static Logger logger = Logger.getLogger(NationService.class);

	@Autowired
	private NationDao nationDao;

	@Autowired
	private BranchDao branchDao;

	public NationGeneDailyTransVo getNationGeneralTransInfo() {
		logger.info("get nation branch general trans");
		NationGeneDailyTransVo nationGeneDailyTrans = new NationGeneDailyTransVo();

		try {
			// 获取最近日期
			String nearestDate = branchDao.getRecentDate();

			List<BranchGeneDailyTrans> branchGeneTransList = nationDao.getBranchGeneTrans(nearestDate);
			BigDecimal totalSqCount = new BigDecimal("0");
			BigDecimal totalJqCount = new BigDecimal("0");
			BigDecimal totalNfcTransCount = new BigDecimal("0");
			BigDecimal totalQrcTransCount = new BigDecimal("0");
			BigDecimal totalOfflineTransCount = new BigDecimal("0");
			List<TransDist> branch_qrc_trans_count_list = new ArrayList<TransDist>();
			List<TransDist> branch_nfc_trans_count_list = new ArrayList<TransDist>();
			List<TransDist> branch_offline_trans_count_list = new ArrayList<TransDist>();
			List<TransDist> branchTransCountList = new ArrayList<TransDist>();
			List<TransDist> branchTransSumList = new ArrayList<TransDist>();
			List<TransDist> branchSqCountList = new ArrayList<TransDist>();
			List<TransDist> branchJqCountList = new ArrayList<TransDist>();
			List<TransDist> branchMchntCountList = new ArrayList<TransDist>();

			for (BranchGeneDailyTrans branchGeneTrans : branchGeneTransList) {

				// 累计商圈数，街区数，nfc交易笔数，二维码交易笔数
				totalSqCount = totalSqCount.add(branchGeneTrans.getSq_count());
				totalJqCount = totalJqCount.add(branchGeneTrans.getJq_count());
				totalNfcTransCount = totalNfcTransCount.add(branchGeneTrans.getNfc_trans_count());
				totalQrcTransCount = totalQrcTransCount.add(branchGeneTrans.getQrc_trans_count());
				totalOfflineTransCount = totalOfflineTransCount.add(branchGeneTrans.getOffline_trans_count());

				String branchCd = branchGeneTrans.getCup_branch_ins_id_cd();
				// 二维码交易笔数
				TransDist branchQrcTransCount = new TransDist();
				branchQrcTransCount.setName(branchCd);
				branchQrcTransCount.setValue(branchGeneTrans.getQrc_trans_count());
				branch_qrc_trans_count_list.add(branchQrcTransCount);
				// NFC交易笔数
				TransDist branchNfcTransCount = new TransDist();
				branchNfcTransCount.setName(branchCd);
				branchNfcTransCount.setValue(branchGeneTrans.getNfc_trans_count());
				branch_nfc_trans_count_list.add(branchNfcTransCount);
				// 线下交易笔数
				TransDist branchOfflineTransCount = new TransDist();
				branchOfflineTransCount.setName(branchCd);
				branchOfflineTransCount.setValue(branchGeneTrans.getOffline_trans_count());
				branch_offline_trans_count_list.add(branchOfflineTransCount);

				// 交易笔数列表
				TransDist branchTransCount = new TransDist();
				branchTransCount.setName(branchCd);
				branchTransCount.setValue(branchGeneTrans.getAll_trans_count());
				branchTransCountList.add(branchTransCount);

				// 交易金额列表
				TransDist branchTransSum = new TransDist();
				branchTransSum.setName(branchCd);
				branchTransSum.setValue(branchGeneTrans.getAll_trans_sum());
				branchTransSumList.add(branchTransSum);

				// 商圈数列表
				TransDist branchSqCount = new TransDist();
				branchSqCount.setName(branchCd);
				branchSqCount.setValue(branchGeneTrans.getSq_count());

				branchSqCountList.add(branchSqCount);

				// 街区数列表
				TransDist branchJqCount = new TransDist();
				branchJqCount.setName(branchCd);
				branchJqCount.setValue(branchGeneTrans.getJq_count());

				branchJqCountList.add(branchJqCount);

				// 商户数列表
				TransDist branchMchntCount = new TransDist();
				branchMchntCount.setName(branchCd);
				branchMchntCount.setValue(branchGeneTrans.getMchnt_count());

				branchMchntCountList.add(branchMchntCount);

			}

			nationGeneDailyTrans.setSq_count(totalSqCount);
			nationGeneDailyTrans.setJq_count(totalJqCount);
			nationGeneDailyTrans.setNfc_trans_count(totalNfcTransCount);
			nationGeneDailyTrans.setQrc_trans_count(totalQrcTransCount);
			nationGeneDailyTrans.setOffline_trans_count(totalOfflineTransCount);

			nationGeneDailyTrans.setBranch_qrc_trans_count_list(branch_qrc_trans_count_list);
			nationGeneDailyTrans.setBranch_nfc_trans_count_list(branch_nfc_trans_count_list);
			nationGeneDailyTrans.setBranch_offline_trans_count_list(branch_offline_trans_count_list);

			nationGeneDailyTrans.setBranch_sq_count_list(branchSqCountList);
			nationGeneDailyTrans.setBranch_jq_count_list(branchJqCountList);
			nationGeneDailyTrans.setBranch_mchnt_count_list(branchMchntCountList);
			nationGeneDailyTrans.setBranch_trans_count_list(branchTransCountList);
			nationGeneDailyTrans.setBranch_trans_sum_list(branchTransSumList);

			nationGeneDailyTrans.setNearestDate(nearestDate);
			nationGeneDailyTrans.setState(Constant.SUCCESS);
		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			nationGeneDailyTrans.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			nationGeneDailyTrans.setState(Constant.INTER_ERROR);
		}

		return nationGeneDailyTrans;
	}

	public NationBranchTransVo getTopBranchTransInfo(String date, String type) {
		logger.info("get  TOP branch,date=" + date + ",type=" + type);
		NationBranchTransVo NationTopBranchTrans = new NationBranchTransVo();

		try {

			List<BranchDailyTrans> branchTransList = nationDao.getTop5Branch(date, type);
			NationTopBranchTrans.setBranch_trans_list(branchTransList);
			NationTopBranchTrans.setState(Constant.SUCCESS);
		} catch (RuntimeException e) {
			logger.error("internal sql error!", e);
			NationTopBranchTrans.setState(Constant.INTER_ERROR);

		} catch (Exception e) {
			logger.error("internal error!", e);
			NationTopBranchTrans.setState(Constant.INTER_ERROR);
		}

		return NationTopBranchTrans;
	}

}
